//index.js
//获取应用实例

const app = getApp()
const util = require('../../utils/util.js')


Page({
  data: {
    imgUrls: [
      // '../../images/bg1.jpg',
      // '../../images/bg2.jpg',
      // '../../images/bg3.jpg
      'http://xuzhenglucky.com.cn/images/bg1.jpg',
      'http://xuzhenglucky.com.cn/images/bg2.jpg',
      'http://xuzhenglucky.com.cn/images/bg3.jpg',   
    ],
    list :[
      {
        id: 'wendu',
        imgUrls: '../../images/wendu.png',
        name: '温度',
        value:'0.0',
        unit: '℃'
        
      }, 
      {
        id: 'shidu',
        imgUrls: '../../images/shidu.png',
        name: '湿度',
        value: '0.0',
        unit: '%'
      },
      {
        id: 'dianya',
        imgUrls: '../../images/dianya.png',
        name: '电压',
        value: '0.0',
        unit: 'V'
      },
      {
        id: 'dianliu',
        imgUrls: '../../images/dianliu.png',
        name: '电流',
        value: '0.0',
        unit: 'A'
      },
      {
        id: 'gonglv',
        imgUrls: '../../images/gonglv.png',
        name: '功率',
        value: '0.0',
        unit: 'W'
      },
      {
        id: 'dianneng',
        imgUrls: '../../images/dianneng.png',
        name: '电能',
        value: '0.0',
        unit: 'KWh'
        
      }
    ],
   
  },
  gotoPage: function (e) {
    var page = e.currentTarget.dataset.page;
    console.log(page);
    console.log('../column/' + page + '/' + page);
    
    wx.navigateTo({
      url: '../column/' + page + '/' + page
    });
  },
  
  onLoad: function () {
    var that = this;
    /*
    wx.request({
      url: "https://xuzhenglucky.com.cn/fun",
      data: {},
      header: {
         "Content-Type":"application/json"
      },
      success: function (res) {
        console.log(res);
        var data = res.data;
        //var JSON_OBJ = JSON.parse(data);

        that.setData({
          'list[0].value': data[0],
          'list[1].value': data[1],
          'list[2].value': data[2],
          'list[3].value': data[3],
          'list[4].value': data[4],
          'list[5].value': data[5]
        });


      },
      fail: function (err) {
        console.log(err)
      }
    
    })
*/
    
    wx.connectSocket({
      url: "wss://xuzhenglucky.com.cn"
      // url: "ws://123.207.44.237:8080"
    })

    wx.onSocketOpen(function (res) {
      console.log('webSocket open');
      
      wx.sendSocketMessage({

        data: "update"
      })
 
      // let p = new Promise(function (resolve, reject) {
      //   //做一些异步操作
      //   setTimeout(function () {
      //     console.log('执行完成');
      //     resolve('随便什么数据');
      //     //reject('出错');
      //   }, 2000);
      // });

    });

    wx.onSocketMessage(function (res) {
      //console.log('Message rev');
      console.log(res.data);
      var JSON_OBJ = JSON.parse(res.data);
      console.log(JSON_OBJ);
    
        that.setData({
          'list[0].value': JSON_OBJ.wendu,
          'list[1].value': JSON_OBJ.shidu,
          'list[2].value': JSON_OBJ.dianya,
          'list[3].value': JSON_OBJ.dianliu,
          'list[4].value': JSON_OBJ.gonglv,
          'list[5].value': JSON_OBJ.dianneng
        });

      
    });
   
  },

})
